package com.voltmao.service.application.sys;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.voltmao.persistent.mapper.SysMessageMapper;
import com.voltmao.persistent.model.SysMessage;
import com.voltmao.persistent.model.SysMessageCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.NumberUtils;
import com.voltmao.webpro.utils.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统消息服务管理接口
 *
 * @author zhanglin
 * @date 2018/4/22
 */
@Service
public class MessageServiceImpl {
    private static final Logger testLogger = LoggerFactory.getLogger("MY_TEST_LOGGER");

    @Autowired
    private SysMessageMapper sysMessageMapper;


    /**
     * 后台新增一条消息
     * @param from 发送者ID,
     * @param to 接收者ID
     * @param msgTitle 消息标题
     * @param msgText 消息文本html格式
     * @param url 响应地址
     * @param msgType 消息所属类型
     */
    public void addMsg(String from, String to, String msgTitle, String msgText, String url, int msgType) {
        testLogger.info("from:"+from+"|| "+"to:"+to+"||"+"msgText:"+msgText+"||"+"url:"+url);
        SysMessage sm = new SysMessage();
        sm.setDataId(StringUtils.uuid36());
        sm.setCreateDate(new Date());
        sm.setIsDel(0);
        sm.setIsRead(0);
        sm.setForCorpEmployesUserFrom(from);
        sm.setForCorpEmployesUserTo(to);
        sm.setMsgText(msgText);
        sm.setMsgTitle(msgTitle);
        sm.setActionUrl(url);
        sm.setForDicMsglx(msgType);
        sysMessageMapper.insert(sm);
    }

    /**
     * 修改消息为已读
     * 新增：msgtext,from,to,url
     * 修改：dataId，isRead
     * @param sm 通知对象
     */
    public void insertOrUpdateMsg(SysMessage sm) {
        sm.setUpdateDate(new Date());
        sm.setIsRead(1);
        sysMessageMapper.updateByPrimaryKeySelective(sm);
    }

    /**
     * 获取接收者消息未读数量
     * @param userId 接收者id
     * @return 未读数量
     */
    public int countMsgUnRead(String userId) {
        SysMessageCriteria example = new SysMessageCriteria();
        SysMessageCriteria.Criteria c =example.createCriteria();
        c.andIsReadEqualTo(0);
        c.andIsDelEqualTo(0);
        if(!StringUtils.isEmpty(userId)){
            c.andForCorpEmployesUserToEqualTo(userId);
        }
        return sysMessageMapper.countByExample(example);
    }

    /**
     * 合同表为主表，合同分页数据及查询
     * 附属了参数表，审核表信息
     * @param params 查询参数集合
     * @return DataPage分页集合
     */
    public DataPage selectDataPageMsgs(Map<String,Object> params) {
        testLogger.info("待查询参数："+params.toString());
        System.out.println("待查询参数："+params.toString());
        DataPage dataPage = new DataPage();
        int pageIndex = StringUtils.parseInt(params.get("pageNumber")+"", 1);
        int pageSize = StringUtils.parseInt(params.get("pageSize")+"", 20);

        SysMessageCriteria example = new SysMessageCriteria();
        SysMessageCriteria.Criteria c =example.createCriteria();
        c.andIsDelEqualTo(0);
        if(!StringUtils.mapIsEmpty(params)){
            String userTo = "userTo";
            if(StringUtils.isNotEmpty(params.get(userTo))) {
                //接收者id查询
                c.andForCorpEmployesUserToEqualTo(params.get(userTo)+"");
            }
            String userFrom = "userFrom";
            if(StringUtils.isNotEmpty(params.get(userFrom))) {
                //发送者id查询
                c.andForCorpEmployesUserFromEqualTo(params.get(userFrom)+"");
            }
            String isRead = "isRead";
            if(StringUtils.isNotEmpty(params.get(isRead))) {
                //是否已读
                c.andIsReadEqualTo(NumberUtils.parseInt(params.get(isRead)+"", 0));
            }
            String msgType = "forDicMsglx";
            if(StringUtils.isNotEmpty(params.get(msgType))) {
                //消息类型
                c.andForDicMsglxEqualTo(NumberUtils.parseInt(params.get(msgType)+"", 0));
            }
        }
        example.setPageSize(pageSize);
        example.setLimitStart((pageIndex - 1) * pageSize);
        example.setOrderByClause("sys_message.create_date desc");
        List<Map<String, Object>> results = sysMessageMapper.selectMapByExample(example);
        dataPage.pageindex= pageIndex;
        dataPage.pagesize = pageSize;
        dataPage.rows = results;
        dataPage.total = sysMessageMapper.countByExample(example);

        return dataPage;
    }
}
